package Others;

public class _504_Base7 {
    public String convertToBase7(int num) {
        if (num == 0) {
            return "0";
        }
        String base7 = "";
        int multi = 1;
        int count = 0;
        while (multi <= Math.abs(num)) {
            multi*=7;
            count++;
        }
        multi/=7;
        int copy = Math.abs(num);
        int bits = 0;
        while (copy != 0) {
            base7 += copy/multi;
            copy = copy % multi;
            multi /= 7;
            bits++;
        }
        while (count > bits) {
            base7 += "0";
            bits++;
        }
        return num<0?("-"+base7):base7;
    }

    //reference solution:use string's method
    public String convertToBase7_1(int num) {
        return Integer.toString(num, 7);
    }
}
